home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / basic / ubmalm.zip / malm.doc < prev    next >
Text File  |  1989-04-25  |  20KB  |  428 lines

  1.  
  2.  
  3. 14 March 1991
  4.  
  5. Procedures  ( File : ProcedureList )
  6. All procedures/functions written by Donald E. G. Malm and copyright by him. 
  7.  
  8. For each procedure/function we normally give its Pascal heading, together with 
  9. other pertinent information.  The UBASIC versions are like the Pascal versions, 
  10. using the same variable names.  The procedures are listed alphabetically,
  11. with the separation of the Pascal routines into the three files BODY 1,2, and 3
  12. indicated.  (The UBASIC subroutines are in individual files.)  There are minor 
  13. differences between the UBASIC and Pascal versions, chiefly because UBASIC has 
  14. no Boolean or character variables.
  15.  
  16.  
  17.  
  18. **************************    BODY1   *************************************
  19.  
  20. PROCEDURE BWppt1(n : number; VAR f : integer);
  21. { Baillie-Wagstaff Lucas pseudoprime test.  See their paper.  We assume
  22.   n is odd and > 11.  D is chosen by method "A".  f is 1 to indicate that n 
  23.   is a probable prime, 0 for composite, and -1 if n is a square.  Requires
  24.   unit MultiP.  Calls Add, Sub, Mul, Dvd, Dvdby2, Equal, Isqrt, Jacobi, and
  25.   Modd. }
  26. { 6 June 1990 }
  27. The article is "LUCAS PSEUDOPRIMES", Robert Baillie and Samuel S. Wagstaff, Jr., 
  28. Math. Comp., v. 35 (1980) pp. 1391-1417.
  29.  
  30.  
  31. PROCEDURE BWppt2(n : number; VAR f : integer);
  32. { Baillie-Wagstaff strong Lucas pseudoprime test.  See their paper.  We assume 
  33.   n is odd and > 11.  D is chosen by method "A*".  f is 1 to indicate that n
  34.   is a probable prime, 0 for composite and -1 if n is a square.  Requires unit
  35.   MultiP.  Calls Add, Sub, Mul, Dvd, Dvdby2, Isqrt, Equal, Modd, Jacobi, and 
  36.   Spwr. }
  37. { 6 June 1990  }
  38.  
  39.  
  40.  
  41. UBASIC: There is a subroutine Cgcd(A,B,&C,&Ca,&Cb). This is part of the unit
  42. MultiP in the Pascal versions.
  43.  
  44.  
  45.  
  46. PROCEDURE Chinese(n:longint; VAR A,B,M:Chn; VAR soln, modulus : number);
  47. { Algorithm for Chinese remaindering of the system A[i] X = B[i] (mod M[i]),
  48.   1 = 1,..,n.  It is NOT assumed that the moduli M[i] are pairwise relatively
  49.   prime.  Soln holds the solution. If there is no solution, modulus = 0,
  50.   otherwise modulus is the modulus of uniqueness of the solution.  Variables A,
  51.   B, and M are input variables; they are VAR only for efficiency.  The type Chn
  52.   is imported from the unit MultiP.  Requires MultiP.  Calls Fcgcd, Dvd, Mul,
  53.   Sub, Add, and Mod. Uses the type Chn.  }        
  54. { 4 August. 1990 }
  55.  
  56.  
  57.  
  58. PROCEDURE Confrat(VAR C : tenr; leng : longint; VAR a,b : number);
  59. { From the finite continued fraction C this procedure calculates the
  60.   rational a/b that corresponds.  Reverses the procedure Ratconf.  Parameter
  61.   C is VAR only for efficiency; it is an input parameter.  leng is the last
  62.   index used in C.  Note that it is assumed that leng >= 0.  Requires unit
  63.   MultiP, including type tenr.  Calls Mul and Add. }
  64. { 7 Jan. 1990 }
  65.  
  66.  
  67. PROCEDURE Elcfctr(n,c1,c2,a : number; VAR f : number);
  68. { Elliptic curve method to factorize n, returning a factor in f.  The
  69.   parameters c1,c2, and a are chosen randomly - more than one choice
  70.   may be necessary.  Requires unit MultiP.  Calls Add, Sub, Mul, Modd,
  71.   and Gcd.  }
  72. { 14 March 1991 }
  73.  
  74.  
  75. PROCEDURE Fermat(a : number; ubnd : longint; VAR f,g : number);
  76. {  Fermat's method of factoring a.  ubnd limits the number of iterations.
  77.    The factors (or -1 if ubnd is reached) are placed in f and g.  a must
  78.    be positive and odd.  Requires Unit MultiP.  Calls Isqrt, Mul, Add, Sub,
  79.    Equal, and Dvdby2. }
  80. {  30 Dec. 1989 }
  81.  
  82.  
  83.  
  84. UBASIC: There is a subroutine Fcgcd(A,B,&C,&Ca).  This is part of the unit
  85. MultiP in the Pascal versions.
  86.  
  87.  
  88.  
  89. PROCEDURE Gpcftoq(VAR CF : tenr; leng1,leng2 : longint; VAR A,B,C : number);
  90. { General periodic continued fraction to quadratic routine.  CF contains the
  91.   continued fraction - indexes 0 through leng1 contain the first part before
  92.   the period while indexes leng1+1 through leng2 contain the complete period.
  93.   It is assumed that the continued fraction represents a positive number.
  94.   CF is an input variable, made VAR for efficiency. A, B, and C return the
  95.   coefficients of the quadratic satisfied by the continued fraction z : 
  96.   A z^2 + Bz + C = 0.  There is no error checking to detect improper input.  
  97.   Requires unit MultiP, including type tenr.  Calls Mul, Add, Sub, Gcd, and Dvd. }   
  98. { 8 Jan. 1990 }
  99.  
  100.  
  101. PROCEDURE Lambda(VAR P, M : triala; k : longint; VAR Lam : number);
  102. { Returns in Lam the value of Carmichael's function - the minimum universal
  103.   exponent.  P, M, and k have the same meaning as in Sigma, and P and M are
  104.   input varaibles - VAR only for efficiency.  Defines by  convention
  105.   Lambda(1) = 0.  Requires unit MultiP, including type triala.  Calls Mul, Sub,
  106.   Dvd, and Gcd.  }
  107. { 2 May 1990 }
  108.  
  109.  
  110. PROCEDURE LDiosys(r,c : integer; VAR A,V : NumMat; VAR soln : Boolean;
  111.                     VAR rank : integer);                    
  112. { Solves the linear Diophantine system represented by the (augmented)
  113.   matrix A.  Matrix V contains a record of the manipulations.  A is r by c+1,
  114.   while V is c by c.  Soln is TRUE if there is a solution, otherwise FALSE.
  115.   Rank is the rank of the system.  Contains the function Pivotind.  Requires
  116.   unit MultiP.  Calls Sub, Mul, Dvd, Less, and Modd. Type NumMat is supplied
  117.   by MultiP. }
  118. { 16 June 1990 }
  119. This is the algorithm described in "A COMPUTER LABORATORY MANUAL FOR NUMBER
  120. THEORY, by Donald E. G. Malm, COMPRess, 1980.
  121.  
  122.  
  123. PROCEDURE Lehmer(p,q : number; VAR r : number);
  124. { D. H. Lehmer's method of solving x^2 = q (mod p), assuming that p is an
  125.   odd prime and q is a quadratic residue modulo p.  The solution is 
  126.   returned in r.  Requires unit MultiP.  Calls Add, Sub, Mul, Dvd, Dvdby2,
  127.   Spwr, Equal, Less, Jacobi, and Modd. }
  128. { 7 June 1990 }
  129. The algorithm is described in Lehmer's article "COMPUTER TECHNOLOGY APPLIED 
  130. TO THE THEORY OF NUMBERS", in "STUDIES IN NUMBER THEORY", MAA 1969,
  131. pp. 117 - 151.
  132.  
  133.  
  134. FUNCTION Mobius(VAR M : triala; k : longint) : longint;
  135. { Returns the Mobius function.  M and k have the same meaning as in Tau.
  136.   Requires unit MultiP for the type triala.  Calls no procedures nor functions.}
  137. { 22 April 1990 }
  138. UBASIC supplies this as a function in the language.
  139.  
  140.  
  141.  
  142. ********************************   BODY2   ************************************
  143.  
  144.  
  145. PROCEDURE Mppt(n,Q : number; VAR f : Boolean);
  146. { Implements Malm's probabilistic test for primality (or compositeness).
  147.   Assumes n is odd, not a square, and that (Q|n)=1.  Returns f = TRUE
  148.   if n is probably prime, and f = FALSE if n is composite. Requires unit 
  149.   MultiP.  Calls Add,Sub, Mul, Dvdby2, Modd, Gcd, Less, Jacobi, and Equal. }
  150. { 7 June 1990 }
  151.  
  152. This procedure Mppt and the following one Mst are very effective unpublished
  153. probable prime tests devised by the author.
  154.  
  155. PROCEDURE Mst(n : number; VAR f : Boolean);
  156. { Test for probable primality or compositeness devised by Malm.  Returns
  157.   f = TRUE if n is a probable prime, and f = FALSE if n is composite.  Requires
  158.   unit MultiP.  Calls Add, Sub, Mul, Dvdby2, Modd, Isqrt, Gcd, Equal, Jacobi,and
  159.   Less.  Contains a subprocedure Lehmerc, which is the procedure Lehmer adapted
  160.   to input that is not necessarily prime. }
  161. { 21 August 1990 }
  162.  
  163.  
  164. FUNCTION NxtPrm(a: integer): longint;
  165.  {  Returns the smallest prime greater than a, or else 0 if a < 0 or
  166.     a >= 10,000.  Returns an integer, not a number.  Requires unit MultiP
  167.     for the array prm[]. Calls no procedures nor functions. }
  168.  { 20 April 1990 }
  169.  UBASIC supplies this as a part of the language.
  170.  
  171.  
  172. PROCEDURE Pell(d : number; VAR x,y : number; VAR f : longint);
  173. { Uses continued fractions to compute the smallest nontrivial positive
  174.   solution to x^2 - d y^2 = f, where f is 1 or -1.  Note that f is computed
  175.   by the program; it is not your choice.  f = 0 indicates improper input.
  176.   Requires unit MultiP.  Calls Isqrt, Mul, Sub, Add, Dvd, and Equal. }
  177. {  10 January 1990 }
  178.  
  179.  
  180. Procedure Peralta(p,x : number; VAR s : number);
  181. { Implements Peralta's (second) probabilistic algorithm to compute  
  182.   s for which s^2 = x (mod p), assuming that p is an odd prime and that x 
  183.   is a quadratic residue modulo p.  Requires unit MultiP.  Calls Add, Sub,
  184.   Mu